<!-- 组委会 -->
<template>
  <div>
    <div class="title">请完善组委会信息</div>
    <el-form
      ref="api_CreateTrainningCommitteeInput"
      :model="api_CreateTrainningCommitteeInput"
      label-width="0"
      size="mini"
      @submit.native.prevent
    >
      <el-row>
        <el-col :span="24" class="form_item">
          <el-form-item>
            <el-input
              v-model="api_CreateTrainningCommitteeInput.content"
              type="textarea"
              :autosize="{ minRows: 15, maxRows: 15}"
              placeholder="请在此输入组委会的信息。"
            />
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
  </div>
</template>
<script>
import { CopyData, deepCopy } from '@/lib';
import {
  SiSTKWTrainCreateTrainningCommitteeInput
} from '@/lib/form/train/swaggerForm';
import {
  api_POST_TrainTrainningCommittee,
  api_PUT_TrainTrainningCommitteeByTrainningIdTrainningId,
  api_GET_TrainTrainningCommitteeByTrainningIdTrainningId
} from '@/extend/api/trainswaggerApi/app';
export default {
  props: {
    gameId: {
      type: String,
      required: false,
      default: ''
    }
  },
  data() {
    return {
      api_CreateTrainningCommitteeInput: new SiSTKWTrainCreateTrainningCommitteeInput(),
      api_CreateTrainningCommitteeInputDTO: {},
      api_CreateTrainningCommitteeInput_old: {}
    };
  },
  created() {
    if (this.gameId) {
      this.getTrainTrainningCommittee();
    } else {
      this.api_CreateTrainningCommitteeInput_old = deepCopy(this.api_CreateTrainningCommitteeInput);
    }
  },
  methods: {
    getTrainTrainningCommittee() {
      api_GET_TrainTrainningCommitteeByTrainningIdTrainningId(this.gameId).then(res => {
        this.api_CreateTrainningCommitteeInputDTO = res || {};
        if (res) {
          CopyData(this.api_CreateTrainningCommitteeInputDTO, this.api_CreateTrainningCommitteeInput);
          this.api_CreateTrainningCommitteeInput_old = deepCopy(this.api_CreateTrainningCommitteeInput);
        }
      }, () => {
        const _msg = error.error.message || error.message;
        this.$message({ type: 'error', message: _msg });
      });
    },
    submit() {
      return new Promise((resolve, reject) => {
        this.api_CreateTrainningCommitteeInput.trainningId = this.gameId;
        if (this.api_CreateTrainningCommitteeInputDTO.id) {
          this.editGameSetting(this.api_CreateTrainningCommitteeInput).then(res => {
            resolve(res);
          }, error => {
            reject(error);
          });
        } else {
          this.addGameSetting(this.api_CreateTrainningCommitteeInput).then(res => {
            resolve(res);
          }, error => {
            reject(error);
          });
        }
      });
    },
    // 新增设置
    addGameSetting(api_CreateTrainningCommitteeInput) {
      return new Promise((resolve, reject) => {
        api_POST_TrainTrainningCommittee(api_CreateTrainningCommitteeInput).then(res => {
          resolve(res);
          this.$message({ type: 'success', message: '新增组委会信息成功！' });
          this.api_CreateTrainningCommitteeInput_old = deepCopy(this.api_CreateTrainningCommitteeInput);
          this.api_CreateTrainningCommitteeInput = res || {};
        }, error => {
          reject(error);
          this.$message({ type: 'error', message: error.error && error.error.message || '' });
        });
      });
    },
    // 更新设置
    editGameSetting(api_CreateTrainningCommitteeInput) {
      return new Promise((resolve, reject) => {
        api_PUT_TrainTrainningCommitteeByTrainningIdTrainningId(this.gameId, api_CreateTrainningCommitteeInput).then(res => {
          resolve(res);
          this.$message({ type: 'success', message: '更新组委会信息成功！' });
          this.api_CreateTrainningCommitteeInput_old = deepCopy(this.api_CreateTrainningCommitteeInput);
        }, error => {
          reject(error);
          this.$message({ type: 'error', message: error.error && error.error.message || '' });
        });
      });
    }
  }
};
</script>
<style scoped>
  .title{
    font-size: 16px;
    color: #666;
    font-weight: bold;
    margin: 50px 0 20px 0;
  }
</style>
